﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YK.Framework.Repository;

namespace YK.Framework.Security
{
    public class ApplicationService
    {
        IRepository repository;

        public ApplicationService(IRepository repository)
        {
            this.repository = repository;
        }

       
        public int CreateApplication(string applicationName)
        {
            Application application = new Application() { Name = applicationName };           

            repository.Save<Application>(application);

            if (application.Id > 0)
            {
                return 0;
            }
            else
            {
                return 1;
            }

        }

        public Application GetApplication(string applicationName)
        {
            Application application = 
                repository.Query<Application>()
                    .SingleOrDefault(x => x.Name == applicationName);

            return application;
        }

        public bool ApplicationExists(string applicationName)
        {
            int count =
                 repository.Query<Application>()
                     .Where(x => x.Name == applicationName)
                     .Count();
                
            return (count > 0);
         
        }
    }
}